package com.test.mooan.labama.adapter_boom;

import static com.test.mooan.labama.activity_fragment_boom.ln_boom.LnMainActivity_boom.toProductPageFlag;
import static com.test.mooan.labama.activity_fragment_boom.ln_boom.LnMainActivity_boom.clickProduct;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.mooan.labama.R;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.bean_boom.product.ProductListInfo;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;

import java.util.ArrayList;

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ListHolder> {

    @NonNull
    @Override
    public ListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ListHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.page_product_list, parent, false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull ListHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    public static class ListHolder extends RecyclerView.ViewHolder{

        private final Context context;

        private final View noDataView;
        private final LinearLayout productsLayout;

        @SuppressLint("InflateParams")
        public ListHolder(View view){
            super(view);
            context = view.getContext();
            noDataView = view.findViewById(R.id.page_product_list_view_no_data);
            productsLayout = view.findViewById(R.id.page_product_list_layout_products);
        }

        public void bind(int position){
            productsLayout.removeAllViews();

            ArrayList<Product> products = position%2==0 ?
                    ProductListInfo.gtInstance().gtProductList() : ProductListInfo.gtInstance().gtRepayProductList();

            initProductList(products);
        }

        private void initProductList(ArrayList<Product> products){

            if (products.isEmpty()){
                noDataView.setVisibility(View.VISIBLE);
            }else {
                noDataView.setVisibility(View.GONE);

                for (Product product:products){
                    View item = View.inflate(context,R.layout.item_product,null);

                    View detailsView = item.findViewById(R.id.item_product_view_details);

                    TextView nameTv = item.findViewById(R.id.item_product_tv_name);

                    View applyTv = item.findViewById(R.id.item_product_tv_apply);

                    View statusView = item.findViewById(R.id.item_product_view_status);
                    ImageView statusIv = item.findViewById(R.id.item_product_iv_status);
                    TextView statusTv = item.findViewById(R.id.item_product_tv_status);

                    View seeDetailsView = item.findViewById(R.id.item_product_tv_see_details);

                    TextView detailTitle1Tv = item.findViewById(R.id.item_product_tv_detail_title_1);
                    TextView detailValue1Tv = item.findViewById(R.id.item_product_tv_detail_value_1);

                    TextView detailTitle2Tv = item.findViewById(R.id.item_product_tv_detail_title_2);
                    TextView detailValue2Tv = item.findViewById(R.id.item_product_tv_detail_value_2);

                    View lockView = item.findViewById(R.id.item_product_view_locked);

                    nameTv.setText(product.gtName());

                    lockView.setVisibility(product.isActive() ? View.GONE:View.VISIBLE);

                    switch (product.productStatus){

                        case Loan_Reviewing -> {

                            applyTv.setVisibility(View.GONE);
                            statusView.setVisibility(View.VISIBLE);
                            seeDetailsView.setVisibility(View.GONE);

                            statusView.setBackgroundResource(R.drawable.bg_in_review);
                            statusIv.setImageResource(R.drawable.icon_in_review);
                            statusTv.setTextColor(context.getColor(R.color.blue_boom));
                            statusTv.setText(context.getString(R.string.in_review));

                            detailTitle1Tv.setText(context.getString(R.string.loan_amount_with_currency,product.gtCurrency()));
                            detailValue1Tv.setText(product.gtAmount());

                            detailTitle2Tv.setText(context.getString(R.string.loan_term));
                            detailValue2Tv.setText(context.getString(R.string.days_num,product.gtDays()));

                            if (product.isActive()){
                                detailsView.setOnClickListener(v -> {
                                    UserInfoHelper_boom.gInstance().addEventPoint("HOME_REVIEW_PRODUCT_CLICK",product.gtProduct());
                                    HttpManager_boom.getInstance().saveUserPointForBatch();
                                    toProductPageFlag = true;
                                    clickProduct = product;
                                    HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
                                });
                            }

                        }

                        case Order_Pending -> {

                            applyTv.setVisibility(View.GONE);
                            statusView.setVisibility(View.VISIBLE);
                            seeDetailsView.setVisibility(View.GONE);

                            statusView.setBackgroundResource(R.drawable.bg_payment_in_progress);
                            statusIv.setImageResource(R.drawable.icon_payment_in_progress);
                            statusTv.setTextColor(Color.parseColor("#FFF58B40"));
                            statusTv.setText(context.getString(R.string.payment_in_progress));

                            detailTitle1Tv.setText(context.getString(R.string.loan_amount_with_currency,product.gtCurrency()));
                            detailValue1Tv.setText(product.gtAmount());

                            detailTitle2Tv.setText(context.getString(R.string.loan_term));
                            detailValue2Tv.setText(context.getString(R.string.days_num,product.gtDays()));

                            if (product.isActive()){
                                detailsView.setOnClickListener(v -> {
                                    UserInfoHelper_boom.gInstance().addEventPoint("HOME_PENDING_PRODUCT_CLICK",product.gtProduct());
                                    HttpManager_boom.getInstance().saveUserPointForBatch();
                                    toProductPageFlag = true;
                                    clickProduct = product;
                                    HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
                                });
                            }

                        }

                        case Loan_Rejected -> {

                            applyTv.setVisibility(View.GONE);
                            statusView.setVisibility(View.VISIBLE);
                            seeDetailsView.setVisibility(View.GONE);

                            statusView.setBackgroundResource(R.drawable.bg_rejected);
                            statusIv.setImageResource(R.drawable.icon_rejected);
                            statusTv.setTextColor(Color.parseColor("#FFF55240"));
                            statusTv.setText(context.getString(R.string.rejected));

                            detailTitle1Tv.setText(context.getString(R.string.loan_amount_with_currency,product.gtCurrency()));
                            detailValue1Tv.setText(product.gtAmount());

                            detailTitle2Tv.setText(context.getString(R.string.loan_term));
                            detailValue2Tv.setText(context.getString(R.string.days_num,product.gtDays()));

                            if (product.isActive()){
                                detailsView.setOnClickListener(v -> {
                                    UserInfoHelper_boom.gInstance().addEventPoint("HOME_REJECTED_PRODUCT_CLICK",product.gtProduct());
                                    HttpManager_boom.getInstance().saveUserPointForBatch();
                                    toProductPageFlag = true;
                                    clickProduct = product;
                                    HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
                                });
                            }

                        }

                        case Order_Pending_Repayment, Order_Overdue_Repayment -> {

                            applyTv.setVisibility(View.GONE);
                            statusView.setVisibility(View.GONE);
                            seeDetailsView.setVisibility(View.VISIBLE);

                            detailTitle1Tv.setText(context.getString(R.string.loan_amount_with_currency,product.gtCurrency()));
                            detailValue1Tv.setText(product.gtAmount());

                            detailTitle2Tv.setText(context.getString(R.string.loan_term));
                            detailValue2Tv.setText(context.getString(R.string.days_num,product.gtDays()));

                            if (product.isActive()){
                                detailsView.setOnClickListener(v -> {
                                    UserInfoHelper_boom.gInstance().addEventPoint("HOME_REPAY_PRODUCT_CLICK",product.gtProduct());
                                    HttpManager_boom.getInstance().saveUserPointForBatch();
                                    toProductPageFlag = true;
                                    clickProduct = product;
                                    HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
                                });
                            }

                        }

                        default -> {

                            applyTv.setVisibility(View.VISIBLE);
                            statusView.setVisibility(View.GONE);
                            seeDetailsView.setVisibility(View.GONE);

                            detailTitle1Tv.setText(context.getString(R.string.max_amount_with_currency,product.gtCurrency()));
                            detailValue1Tv.setText(product.gtAmount());

                            detailTitle2Tv.setText(context.getString(R.string.lowest_rate));
                            detailValue2Tv.setText(product.gtInterestRate());

                            if (product.isActive()){
                                detailsView.setOnClickListener(v -> {
                                    UserInfoHelper_boom.gInstance().addEventPoint("HOME_NOORDER_PRODUCT_CLICK",product.gtProduct());
                                    HttpManager_boom.getInstance().saveUserPointForBatch();
                                    toProductPageFlag = true;
                                    clickProduct = product;
                                    HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
                                });
                            }

                        }
                    }

                    productsLayout.addView(item);
                }
            }

        }

    }

}
